package mysql

import (
	"fmt"
	"github.com/fantomo/go-iching/config"
	"gorm.io/driver/mysql"
	"gorm.io/gorm"
	"gorm.io/gorm/logger"
)

var DB *gorm.DB

func init() {

	conf := config.GetConf()
	host := conf.Database.Host
	port := conf.Database.Port
	user := conf.Database.User
	password := conf.Database.Password
	database := conf.Database.DB

	dataSource := fmt.Sprintf("%s:%s@tcp(%s:%d)/%s?charset=utf8mb4&parseTime=True&loc=Local", user, password, host, port, database)

	var err error
	var sqlLogger logger.Interface
	if conf.Runtime.ENV == "development" {
		sqlLogger = logger.Default.LogMode(logger.Info)
	}
	DB, err = gorm.Open(mysql.Open(dataSource), &gorm.Config{
		Logger: sqlLogger,
	})
	if err != nil {
		panic(err)
	}
}
